package com.hoshiicloud.mallshop.controller.markermanage;

import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.query.UploadFileResponse;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.mallshop.feign.ShopMarketFeignService;
import com.hoshiicloud.mallshop.service.StoreSlideshowService;
import com.hoshiicloud.market.entity.activities.param.SlideshowAddParam;
import com.hoshiicloud.market.entity.activities.param.SlideshowQueryParam;
import com.hoshiicloud.market.entity.activities.param.SlideshowUpdateParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 *  linjx on 2019/8/26
 */
@RestController
@RequestMapping("/marker-manage/store-slide-show")
@Slf4j
@Api(value = "营销管理-店铺轮播配置",tags = {"营销管理-店铺轮播配置"})
public class StoreSlideshowController {
    private final ShopMarketFeignService marketFeignService;
    private final StoreSlideshowService storeSlideshowService;
    public StoreSlideshowController(ShopMarketFeignService marketFeignService, StoreSlideshowService storeSlideshowService) {
        this.marketFeignService = marketFeignService;
        this.storeSlideshowService = storeSlideshowService;
    }

    @ApiOperation(value = "轮播图图片上传")
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public Response<UploadFileResponse> uploadFile(@RequestPart("file") MultipartFile file) {
        UploadFileResponse uploadFileResponse = storeSlideshowService.uploadFile(file);
        return Response.data(uploadFileResponse);
    }


    @ApiOperation(value = "查询需要跳转的商品信息")
    @GetMapping("/chooseExtSpus")
    public Response chooseExtSpus(@RequestParam("title") String title, HttpServletRequest request) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        return Response.data(storeSlideshowService.chooseExtSpus(title, user.getStoreId()));
    }

    @ApiOperation(value = "保存店铺轮播图信息")
    @PostMapping("/saveSlideshow")
    public Response saveSlideshow(@RequestBody SlideshowAddParam param, HttpServletRequest request) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        param.setCompanyId(user.getCompanyId());
        param.setPlatformId(user.getPlatId());
        param.setStoreId(user.getStoreId());
        param.setCreatedBy(user.getRealName());
        param.setUpdatedBy(user.getRealName());
        param.setUpdatedTime(LocalDateTime.now());
        return marketFeignService.saveSlideshow(param);
    }

    @ApiOperation(value = "根据id查询店铺轮播图信息")
    @GetMapping("/{id}")
    public Response getSlidershowById(@PathVariable("id") Long id) {
        return marketFeignService.querySlideshowById(id);
    }

    @ApiOperation(value = "更新店铺轮播图信息")
    @PutMapping("/updateSlideshow")
    public Response updateSlideshow(@RequestBody SlideshowUpdateParam param, HttpServletRequest request) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        param.setUpdatedBy(user.getRealName());
        return marketFeignService.updateSlideshowById(param);
    }

    @ApiOperation(value = "开启或关闭店铺轮播图信息")
    @PutMapping("/openOrCloseSlideshow")
    public Response openOrCloseSlideshow(@RequestBody SlideshowUpdateParam param, HttpServletRequest request) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        param.setUpdatedBy(user.getRealName());
        return marketFeignService.updateSlideshowById(param);
    }

    @ApiOperation(value = "分页查询轮播图信息")
    @PostMapping("/slidershowPage")
    public Response slidershowPage(@RequestBody SlideshowQueryParam param){
        return Response.data(storeSlideshowService.slidershowPage(param));
    }

    @ApiOperation(value = "根据Id删除店铺轮播图信息")
    @DeleteMapping("/deleted/{id}")
    public Response deletedSlideshowById(@PathVariable("id") Long id){
        return marketFeignService.deletedSlideshowById(id);
    }
}
